package com.moss.cloud.common.file.service.imp;

import com.moss.cloud.common.file.config.StorageConfig;
import com.moss.cloud.common.file.exception.StorageErrorType;
import com.moss.cloud.common.file.exception.StorageException;
import com.moss.cloud.common.file.factory.FileStorageFactory;
import com.moss.cloud.common.file.model.StorageModel;
import com.moss.cloud.common.file.model.UploadFrom;
import com.moss.cloud.common.file.model.UploadRes;
import com.moss.cloud.common.file.model.UploadZoomFrom;
import com.moss.cloud.common.file.operation.FileStorageOperation;
import com.moss.cloud.common.file.service.IStorageService;
import com.moss.cloud.common.file.utils.BooleanHandel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@Service
public class StorageServiceImpl implements IStorageService {
    @Resource
    StorageConfig storageConfig;

    /**
     * 通用文件上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public UploadRes uploadFile(MultipartFile multipartFile) {
        StorageModel storageModel = StorageModel.builder().isBase64(Boolean.FALSE)
                .multipartFile(multipartFile).storageConfig(storageConfig).build();
        FileStorageOperation operation = FileStorageFactory.storageType(storageConfig.getType())
                .orElseThrow(() -> new StorageException(StorageErrorType.STORAGE_ERROR));
        return operation.upload(storageModel);
    }

    /**
     * base65图片上传
     *
     * @param uploadFrom
     * @return
     */
    @Override
    public UploadRes uploadBase64(UploadFrom uploadFrom) {
        BooleanHandel.trueThrow(StringUtils.isBlank(uploadFrom.getImgBase64())).throwMessage(StorageErrorType.FILE_EMPTY);
        BooleanHandel.trueThrow(StringUtils.isBlank(uploadFrom.getName())).throwMessage(StorageErrorType.NAME_EMPTY);
        StorageModel storageModel = StorageModel.builder().isBase64(Boolean.TRUE)
                .imgBase64(uploadFrom.getImgBase64()).name(uploadFrom.getName()).storageConfig(storageConfig).build();
        FileStorageOperation operation = FileStorageFactory.storageType(storageConfig.getType())
                .orElseThrow(() -> new StorageException(StorageErrorType.STORAGE_ERROR));
        return operation.upload(storageModel);
    }

    /**
     * 上传压缩文件，缩略图（只适用图片）
     *
     * @param uploadZoomFrom
     * @return
     */
    @Override
    public UploadRes uploadZoomFile(UploadZoomFrom uploadZoomFrom) {
        BooleanHandel.trueThrow(uploadZoomFrom.getInputStream() == null).throwMessage(StorageErrorType.FILE_EMPTY);
        BooleanHandel.trueThrow(StringUtils.isBlank(uploadZoomFrom.getName())).throwMessage(StorageErrorType.NAME_EMPTY);
        StorageModel storageModel = StorageModel.builder().isBase64(Boolean.TRUE)
                .inputStream(uploadZoomFrom.getInputStream()).name(uploadZoomFrom.getName()).storageConfig(storageConfig).build();
        FileStorageOperation operation = FileStorageFactory.storageType(storageConfig.getType())
                .orElseThrow(() -> new StorageException(StorageErrorType.STORAGE_ERROR));
        return operation.upload(storageModel);
    }

    /**
     * 删除文件
     *
     * @param fileId
     */
    @Override
    public void removeFile(String fileId) {
        StorageModel storageModel = StorageModel.builder().fileId(fileId).storageConfig(storageConfig).build();
        FileStorageOperation operation = FileStorageFactory.storageType(storageConfig.getType())
                .orElseThrow(() -> new StorageException(StorageErrorType.STORAGE_ERROR));
        operation.removeFile(storageModel);
    }
}
